package com.toune.movie.mvp.adapter;

import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.toune.movie.R;
import com.toune.movie.base.myUtil.MyUtil;
import com.toune.movie.base.myview.MusicPlayDialog;
import com.toune.movie.vo.MusicVo;
import com.vondear.rxtool.RxFileTool;
import com.vondear.rxtool.view.RxToast;
import com.vondear.rxui.view.RxTextViewVertical;
import com.yhao.floatwindow.FloatWindow;
import com.yhao.floatwindow.Screen;

import java.util.ArrayList;
import java.util.List;

public class AdapterMusicVo extends BaseQuickAdapter<MusicVo.DataBean, BaseViewHolder> {

    public AdapterMusicVo(int layoutResId, @Nullable List<MusicVo.DataBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final MusicVo.DataBean item) {
        ImageView imageView = helper.getView(R.id.cover_iv);
        Glide.with(mContext).load(item.getPic()).apply(new RequestOptions().centerCrop().placeholder(R.mipmap.holder).error(R.mipmap.holder)).into(imageView);
        RelativeLayout relativeLayout = helper.getView(R.id.play_rl);
        relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (FloatWindow.get() != null) {
                    FloatWindow.get().updateX(Screen.width, 0.8f);
                    FloatWindow.get().updateY(Screen.height, 0.3f);
                }
                final MusicPlayDialog musicPlayDialog = MyUtil.showMusicPlayDialog(mContext, item, null, helper.getLayoutPosition());
                if (musicPlayDialog != null) {
                    musicPlayDialog.setOnSwitchListener(new MusicPlayDialog.OnSwitchListener() {
                        @Override
                        public void onLeft(int pos) {
                            if (musicPlayDialog == null) {
                                return;
                            }
                            if (pos > 0) {
                                int position = pos - 1;
                                musicPlayDialog.resetMusic(mData.get(position), null, position);
                            } else {
                                int position = mData.size() - 1;
                                musicPlayDialog.resetMusic(mData.get(position), null, position);
                            }
                        }

                        @Override
                        public void onRight(int pos) {
                            if (musicPlayDialog == null) {
                                return;
                            }
                            if (mData.size() - 1 > pos) {
                                int position = pos + 1;
                                musicPlayDialog.resetMusic(mData.get(position), null, position);
                            } else {
                                musicPlayDialog.resetMusic(mData.get(0), null, 0);
                            }
                        }
                    });
                }
            }
        });
        helper.setText(R.id.title_tv, "歌名：" + item.getTitle());
        helper.setText(R.id.author_tv, "作者：" + item.getAuthor());
        RxTextViewVertical textViewVertical = helper.getView(R.id.lrc_tv);
        ArrayList<String> lrcs = new ArrayList<>();
        String[] split = item.getLrc().split("\\n");
        for (int i = 0; i < split.length; i++) {
            lrcs.add(split[i]);
        }
        if (lrcs.size() > 8) {
            try {
//                RxFileTool.saveFileUTF8(MyUtil.getAppMusicPath() + "/" + item.getTitle() + "~" + item.getAuthor() + ".lrc", item.getLrc(), false);
                textViewVertical.setTextList(lrcs);
                textViewVertical.setText(10, 5, mContext.getResources().getColor(R.color.black));//设置属性
                textViewVertical.setTextStillTime(3000);//设置停留时长间隔
                textViewVertical.setAnimTime(300);//设置进入和退出的时间间隔
                textViewVertical.setOnItemClickListener(new RxTextViewVertical.OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                    }
                });
                textViewVertical.startAutoScroll();
                textViewVertical.setVisibility(View.VISIBLE);
            } catch (Exception e) {
                e.printStackTrace();
                textViewVertical.setVisibility(View.INVISIBLE);
            }
        } else {
            textViewVertical.setVisibility(View.INVISIBLE);
        }
        helper.getView(R.id.down_mp3_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyUtil.showDownDialog(mContext, item);
            }
        });
    }
}
